<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
      <div id="a1" info="它的内容是1*a">2</div>
      <div id="a2" info="它的内容是2*a">4</div>
      <div id="b1" info="它的内容是1*b+1">5</div>
      <div id="b2" info="它的内容是2*b-1">7</div>
    </div>
    <script>
       var obj = {
          a: 2,
          b: 4
        }
       let newObj = {}
       function updateA1() {
         document.getElementById('a1').innerHTML = newObj.a * 1
       }
       function updateA2() {
         document.getElementById('a2').innerHTML = newObj.a * 2
       }

       function updateB1() {
        document.getElementById('b1').innerHTML = newObj.b * 1 + 1
       }
       function updateB2() {
         document.getElementById('b2').innerHTML = newObj.b * 2 - 1
       }
       const eventEnter = {
         "a": [updateA1, updateA2],
         "b": [updateB1, updateB2]
       }
       function obServer () {
        for (let key in obj) {
            Object.defineProperty(newObj, key, {
                get () {
                    console.log('getter')
                    return obj[key]
                },
                set (newVal) {
                    console.log(`有人修改了${key}属性，新值是:${newVal}`)
                    obj[key] = newVal
                    eventEnter[key].forEach(fn => fn()) // 优雅
                }
            })
        }
       }
       obServer()

    </script>
</body>
</html>